package cn.com.server.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;

import cn.com.server.mongo.dao.MongoDBDao;
import cn.com.server.service.MongoDBService;

@Service
public class MongoDBServiceImpl implements MongoDBService{
    
    @Autowired
    private MongoDBDao mongoDBDao;
    
    @Override
    public void save(Object obj) {
        mongoDBDao.save(obj);
    }

	@Override
	public void insert(Object obj) {
        mongoDBDao.insert(obj);
	}

    @Override
    public <T> T findOne(Class<T> clazz, Query query) {
        return mongoDBDao.findOne(clazz, query);
    }

    @Override
    public <T> List<T> findAll(Class<T> clazz) {
        return mongoDBDao.findAll(clazz);
    }

    @Override
    public <T> List<T> find(Class<T> clazz, Query query) {
        return mongoDBDao.find(clazz, query);
    }

    @SuppressWarnings("unchecked")
	@Override
    public <T> Page<T> getPagination(Class<T> clazz, Query query,
            int currentPage, int pageSize) {
        long recordCount = mongoDBDao.findCount(clazz, query);
        Page<Object> page = new Page<>();
        if(recordCount>0) {
        	List<T> recordList = mongoDBDao.findList(clazz, query, currentPage, pageSize);
        	page.addAll(recordList);
        }
        page.setTotal(recordCount);
        return (Page<T>) page;
    }

    @Override
    public <T> int update(Query query, Update update, Class<T> clazz) {
        return mongoDBDao.update(query, update, clazz);
    }

	@Override
	public boolean remove(Object object) {
		return mongoDBDao.remove(object);
	}

	@Override
	public <T> int remove(Query query, Class<T> clazz) {
		return mongoDBDao.remove(query, clazz);
	}

	@Override
	public String getId(String name) {
		return mongoDBDao.getId(name);
	}


}